package com.lc.qianrenshiye.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.uitls.MyToast;
import com.lc.qianrenshiye.uitls.ZXingUtils;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

/**
 * Created by Administrator on 2016/11/18.
 */
public class PayActivity extends BaseActivity {

    public static final String PAY_FILTER = "pay_filter";
    @BoundView(R.id.pay_qrcode_iv) private ImageView pay_qrcode_iv;
    @BoundView(R.id.pay_money_tv) private TextView pay_money_tv;
    @BoundView(R.id.pay_wv) private WebView pay_wv;

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            MyToast.showToast("支付成功", context);

            BaseApplication.INSTANCE.finishActivity(ConfirmOrderActivity.class, PayActivity.class);

            try {

                //刷新 订单页
                OrderActivity.RefrehOrderCallback refrehOrderCallback= (OrderActivity.RefrehOrderCallback) getAppCallBack(OrderActivity.class);
                refrehOrderCallback.refreshOrder();

                //刷新 商家中心页
                BusinessCenterActivity.RefreshTitleAndHead refreshTitleAndHead = (BusinessCenterActivity.RefreshTitleAndHead) getAppCallBack(BusinessCenterActivity.class);
                refreshTitleAndHead.refresh();

                HomeActivity.RefreshCount refreshCount = (HomeActivity.RefreshCount) getAppCallBack(HomeActivity.class);
                refreshCount.refresh();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);

        setBackTrueOrder();

        setTitleNameOrder(getString(R.string.pay_title_text));

        pay_wv.getSettings().setJavaScriptEnabled(true);

        pay_wv.setWebChromeClient(new WebChromeClient());

        pay_wv.setWebViewClient(new WebViewClient());

        pay_wv.getSettings().setUseWideViewPort(true);

        pay_wv.getSettings().setLoadWithOverviewMode(true);

        Bundle bundle = getIntent().getExtras();

        int payType = bundle.getInt("PayType");

        if (payType == 0) {

            String payMoney = bundle.getString("PayMoney", "");

            pay_money_tv.setText(getString(R.string.pay_money_unit) + payMoney);

            pay_qrcode_iv.setImageBitmap(ZXingUtils.createQRImage(bundle.getString("qrcode"), ScaleScreenHelperFactory.getInstance().getWidthHeight(300), ScaleScreenHelperFactory.getInstance().getWidthHeight(300)));

        } else {

            String url = bundle.getString("url", "");

            pay_wv.setVisibility(View.VISIBLE);
            pay_wv.loadUrl(url);

        }


        try {

            registerReceiver(receiver, new IntentFilter(PAY_FILTER));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//
//            if (receiver != null) {
//
//                unregisterReceiver(receiver);
//
//            }
//
//            Log.e("----", "走返回键");
//
//            finish();
//        }
//
//        return true;
//    }

//    @Override
//    protected void onResume() {
//
//        registerReceiver(receiver, new IntentFilter(PAY_FILTER));
//
//        super.onResume();
//    }
//
    @Override
    protected void onDestroy() {

        if (receiver != null) {

            try {
                unregisterReceiver(receiver);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        super.onDestroy();
    }


}
